package 其他;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author wei.zhao
 * @description:
 * @date 2022年07月04日 13:52
 */
public class 对象拷贝 {

    public static void main(String[] args) {
        Tea tea = new Tea("小红", "三国");
        Stu stu = new Stu("小李", tea);
        //复杂类型--->String，会调用toString()方法！
        Tea copyTea = BeanUtil.copyProperties(stu, Tea.class);
        System.out.println(copyTea);
        // String--->复杂类型会报错，通过CopyOptions.create().ignoreError()可以解决！
        Stu copyStu = new Stu();
        BeanUtil.copyProperties(tea, copyStu, CopyOptions.create().ignoreError());
        System.out.println(copyStu);
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class Stu {
        private String name;
        private Tea ref;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class Tea {
        private String name;
        private String ref;
    }

}
